# Title: 资源池通知文本
# Params: filter_ips|multi_str

from datetime import datetime, timedelta
import python_calamine


def xlsx_to_dict(file: str, sheet_index=0) -> list[dict]:
    with open(file, 'rb') as file:
        workbook = python_calamine.CalamineWorkbook.from_filelike(file)
        _rows = iter(workbook.get_sheet_by_index(sheet_index).to_python())
        headers = list(map(str, next(_rows)))
        _values = [dict(zip(headers, row)) for row in _rows]

    return _values


def main():
    brand_dict = {
        '新华三': "韩力/13985372931",
        '中兴': "袁乾峰/17685115373",
        '烽火': "杨清/18302568060",
        '浪潮': "张宇峰/13595797013",
        "曙光": "肖禾/13595151692",
        "宝德": "陆游/18798794454",
        "华为": "张寅/18985501032",
        "超聚变": "余骥力/13595774261",
    }

    base_data = xlsx_to_dict("depository/台账/IT云贵池设备台账.xlsx", 0)
    used_server_data = xlsx_to_dict("depository/台账/裸金属服务器报表.xlsx", 0)
    gpu_server_data = xlsx_to_dict("depository/台账/GPU裸金属服务器报表.xlsx", 0)
    _node_filter = ['裸金属', 'GPU裸金属', '存储节点', '分布式块存储', '管理节点', '宿主机']

    base_data = list(filter(lambda x: x['设备类型'] == '服务器' and x['节点类型'] in _node_filter, base_data))

    manage_ip_list = ip_list.split()


    output = []
    for index, _ip in enumerate(manage_ip_list):
        if not _ip:
            continue
        base_item = list(
            filter(lambda x: x['管理IP'].strip() == _ip.strip() or x['IPMIIP'].strip() == _ip.strip(),
                   base_data))
        if not base_item:
            return '没有查到台账信息：' + _ip
        manage_ip = base_item[0]['管理IP']
        a = base_item[0]
        b = {}
        used_server_item = list(filter(lambda x: x['管理网IP'].strip() == manage_ip, used_server_data))
        # _console(used_server_item)
        if used_server_item:
            b = used_server_item[0]
        else:
            gpu_server_item = list(filter(lambda x: x['管理网IP'].strip() == manage_ip,gpu_server_data))
            if gpu_server_item:
                b = gpu_server_item[0]
        # _console(b)
        res = a['品牌']
        # _console(res)
        # _console(brand_dict.get(res.strip()))
        output_lines = [
            f"{index + 1}. 【通知】",
            "【变更类型】故障处理类",
            "【所在资源池】贵阳资源池",
            "【工单号】",
            "【变更名称】更换故障硬盘",
            f"【变更内容】{a['设备名称']}更换故障硬盘",
            "【变更原因】硬盘故障，需要更换",
            "【责任专业】主机组",
            f"【变更对象】管理IP：{a['管理IP']}",
            "【是否中断服务】否",
            f"【执行人/电话】：{brand_dict.get(res.strip())}",
            f"【开始时间】{(datetime.now() + timedelta(minutes=90)).strftime('%Y/%m/%d/%H:00')}",
            f"【结束时间】{datetime.now().strftime('%Y/%m/%d/')}",
            "【变更结果】",
            f"【次日保障人员】{_name}/{_phone}",
            f"【所属业务】{b.get('业务系统')}",
            "【租户沟通情况】已与租户沟通，租户反馈可以更换",
        ]
        if start_time:
            output_lines[-6] = f"【开始时间】{start_time}"
        if complete:
            output_lines[-5] = f"【结束时间】{datetime.now().strftime('%Y/%m/%d/%H:%M')}"
            output_lines[-4] = f"【结束时间】硬件已更换，测试正常"
        output.append('\n'.join(output_lines))
    return '\n'.join(output)


